package com.yunfeng.supercall.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;

import com.ly.quickdev.library.utils.JsonUtils;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;

public class SearchUserActivity extends
		BaseStickyHeaderListActivity<SuperFriendsBean> {

	private EditText mEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mEditText = findView(R.id.et);
		mEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				searchData(s.toString());
			}
		});
		mLoadingView.setVisibility(View.GONE);
	}

	private void searchData(String s) {
		JSONObject json = getBaseParams(getActivity());
		try {
			json.put("i_key", s);
			json.put("i_type", 0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(FUNCTION_SEARCH_USER, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub
				mList.clear();
				setListAdapter();
			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				mList.clear();
				mList.addAll(JsonUtils.parseList(data, SuperFriendsBean.class));
				setListAdapter();
			}
		});
	}

	public void cancel(View v) {
		finish();
	}

	@Override
	public void setContentView(int layoutResID) {
		// TODO Auto-generated method stub
		super.setContentView(R.layout.activity_search_user);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		startActivity(new Intent(this, FriendsDetailActivity.class).putExtra(
				KEY_DATA, mList.get(position)));
	}

	@Override
	public void loadData() {
		// TODO Auto-generated method stub

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = getLayoutInflater().inflate(
					R.layout.list_item_system_friends, null);
		}
		SuperFriendsBean.setupSingleLineContactsView(convertView,
				mList.get(position));
		return convertView;
	}

}
